<template>
  <el-dialog
    :title="!dataForm.askId ? '请假申请' : '批假审核'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="请假人"  v-if="dataForm.askId" >
        <el-input v-model="dataForm.realName" disabled></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="beginTime" v-if="!dataForm.askId">
          <el-date-picker
            v-model="dataForm.beginTime" type="datetime" placeholder="选择日期时间" default-time="9:00:00" >
          </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime" v-if="!dataForm.askId">
        <el-date-picker v-model="dataForm.endTime" type="datetime" placeholder="选择日期时间" default-time="18:00:00">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="请假事由" prop="askReason" >
        <el-input v-model="dataForm.askReason" placeholder="请假事由" :disabled="dataForm.askId !== 0"></el-input>
      </el-form-item>
      <el-form-item label="审核状态" prop="askStatus" v-if="dataForm.askId">
        <template>
          <el-radio v-model="dataForm.askStatus" label="1">同意</el-radio>
          <el-radio v-model="dataForm.askStatus" label="2">不同意</el-radio>
        </template>
      </el-form-item>
      <el-form-item label="审核回复" prop="askReturn" v-if="dataForm.askId">
        <el-input v-model="dataForm.askReturn" placeholder="审核回复"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        askId: 0,
        userId: '',
        beginTime: '',
        endTime: '',
        askStatus: '1',
        askReviewer: '',
        askReturn: '',
        askReason: '',
        realName: '' //vo
      },
      dataRule: {
        beginTime: [
          {required: true, message: '开始时间不能为空', trigger: 'blur'}
        ],
        endTime: [
          {required: true, message: '结束时间不能为空', trigger: 'blur'}
        ],
        askReason: [
          {required: true, message: '请假事由不能为空', trigger: 'blur'}
        ],
        askReturn: [
          {required: true, message: '审核回复不能为空', trigger: 'blur'}
        ],
        askStatus: [
          {required: true, message: '审核不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.askId = id || 0
      // console.log("拿到的id ",this.dataForm.askId)
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        // 新增操作时清理数据Form中的数据
        if (!this.dataForm.askId) {
          this.dataForm.userId = '';
          this.dataForm.beginTime = '';
          this.dataForm.endTime = '';
          this.dataForm.askStatus = '';
          this.dataForm.askReviewer = '';
          this.dataForm.askReturn = '';
          this.dataForm.askReason = '';
          this.dataForm.realName = '';
        }
        if (this.dataForm.askId) {
          this.$http({
            url: this.$http.adornUrl(`/ask/reply/info/${this.dataForm.askId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.userId = data.reply.userId
              this.dataForm.beginTime = data.reply.beginTime
              this.dataForm.endTime = data.reply.endTime
              // this.dataForm.askStatus = data.reply.askStatus
              this.dataForm.askReviewer = data.reply.askReviewer
              this.dataForm.askReturn = data.reply.askReturn
              this.dataForm.askReason =data.reply.askReason
              this.dataForm.realName =data.reply.realName
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/ask/reply/${!this.dataForm.askId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'askId': this.dataForm.askId || undefined,
              'userId': this.$store.state.user.id,
              'beginTime': this.dataForm.beginTime,
              'endTime': this.dataForm.endTime,
              'askStatus': this.dataForm.askStatus,
              'askReviewer': this.$store.state.user.realName, //审核人
              'askReturn': this.dataForm.askReturn,
              'askReason': this.dataForm.askReason
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
